<?php


namespace app\admin\controller;


use app\BaseController;
use think\facade\Db;
use think\facade\View;

class banner extends BaseController
{
    //banner页面
    public function bannerList(){
        $list=Db::name('banner')
            ->where('delete','0')
            ->order('id','desc')
            ->paginate(4);
        $page = $list->render();
        $count = Db::name('banner')
            ->where('delete','0')
            ->count();
        View::assign('count',$count);
        View::assign('list',$list);
        View::assign('page',$page);
        return View::fetch();
    }
    //banner添加
    public function bannerInsert(){
        return View::fetch();
    }
    public function bannerInserts(){
        $data = [
            'img'=>"http://www.liberation:1314".input('img'),
            'link'=>input('link'),
            'position'=>input('position')
        ];
        $insert=Db::name('banner')->insert($data);
        if ($insert !== false){
            $arr=[
                'code'=>100,
                'msg'=>'添加成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'添加失败'
            ];

        }
        return json($arr);
    }
    //image
    public function upload(){
        $file=request()->file('photo');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
        $result=[
            'code'=>1,
            'imgpath'=>$savename
        ];
        return json($result);
    }
    //banner删除
    public function bannerDelete(){
        $data=[
            'id'=>input('id'),
            'delete'=>input('delete')?0:1
        ];
        $info=Db::name('banner')->update($data);
        if ($info!==false)
        {
            return json(['code'=>101,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    //banner修改
    public function bannerUpdate(){
        $id=input('id');
        $info=Db::name('banner')->where('id',$id)->find();
        View::assign('info',$info);
        return View::fetch();
    }
    public function bannerUpdates(){
        $data=[
            'id'=>input('id'),
            'img'=>input('img'),
            'link'=>input('link'),
            'position'=>input('position')
        ];
        $info=Db::name('banner')->update($data);
        if ($info!=false){
            $arr=[
                'code'=>100,
                'msg'=>'修改成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'修改失败'
            ];
        }
        return json($arr);
    }
    //banner状态
    public function bannerStatus(){
        $data=[
            'id'=>input('id'),
            'status'=>input('status')?0:1
        ];
        $info=Db::name('banner')->update($data);
        if ($info!==false)
        {
            return json(['code'=>100,'msg'=>'启用成功']);
        }else{
            return json(['code'=>101,'msg'=>'禁用成功']);
        }
    }
    //图片恢复
    public function bannerYsc(){
        $info = Db::name('banner')->where('delete',1)->order('id desc')->select()->toArray();
        $count = Db::name('banner')->where('delete',1)->count();
        View::assign('count',$count);
        View::assign('info',$info);
        return View::fetch();
    }
    public function bannerDeletes(){
        $data=[
            'id'=>input('id'),
            'delete'=>input('delete')?0:1
        ];
        $info = Db::name('banner')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'恢复成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'恢复失败'
            ];
        }
        return json($arr);
    }
}